package com.retch.wenchengmao.sqlitedeme2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * author:wenchengmao
 * time:  2017/3/20.
 * email: wenchengmao@yeah.net
 */

public class DeleteOneDataActivity extends Activity implements View.OnClickListener {
    private TextView info_id,name,tuition,price;
    private Button delete;
    int id;
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_one_data);
        initView();
        receiveData();
        delete();
    }

    public void initView()
    {
        info_id=(TextView)findViewById(R.id.info_id);
        name=(TextView)findViewById(R.id.name);
        tuition=(TextView)findViewById(R.id.tuition);
        price=(TextView)findViewById(R.id.price);
        delete=(Button)findViewById(R.id.delete_data);
        delete.setOnClickListener(this);

    }

    public void receiveData()
    {
        Intent intent=getIntent();
        if(intent!=null){
            id=intent.getExtras().getInt("id");
            String names=intent.getExtras().getString("name");
            double tuitions=intent.getExtras().getDouble("tuition");
            float prices=intent.getExtras().getFloat("price");
            info_id.setText(String.valueOf(id));
            name.setText(names);
            tuition.setText(String.valueOf(tuitions));
            price.setText(String.valueOf(prices));
        }
    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.delete_data:
                delete();
                finish();
                break;
            default:
                break;
        }
    }

    public void delete()
    {
        MySQLiteHelper mySQLiteHelper= new MySQLiteHelper(DeleteOneDataActivity.this, "db_test5", null, 1);
        mySQLiteHelper.deleteOneData(Student.class,id);
        Toast.makeText(DeleteOneDataActivity.this,"已删除选中的数据",Toast.LENGTH_LONG).show();
    }

}
